<?php
/* ******************************
 *	Partner Cadre aJax actions
 * ******************************
 */
 
include('../../../includes/classes/core.php');
$action = $_REQUEST['act'];
$error = '';
$data = '';

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $cadre_id	= $_REQUEST['id'];
		$page		= GetPage(GetCadre($cadre_id));
		
		$data		= array('page'	=> $page);

        break;
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    $part_id = $_REQUEST['part_id'];
	    
	    $rResult = mysql_query("SELECT `id`,
									   `user_id`,
									   `f_name`,
									   `l_name`,
									   `position`
								FROM `partner_contact_info`
								WHERE `partner_id` = $part_id");
	    
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

        break;
    case 'save_cadre':
		$cadre_id 		= $_REQUEST['id'];
		$part_id 		= $_REQUEST['p_id'];
					
    	$user_id		= $_REQUEST['ui'];
		$c_fname	 	= $_REQUEST['cfn'];
		$c_lname 		= $_REQUEST['cln'];
		$c_position		= $_REQUEST['cp'];
		$c_contact		= $_REQUEST['cc'];
		$c_phone		= $_REQUEST['cph'];
		$c_m_phone 		= $_REQUEST['cmp'];
		
		$c_sale			= $_REQUEST['cs'];
		$c_limit		= $_REQUEST['cl'];
		
		if($c_fname != '' && $c_lname != '' && $cadre_id == ''){
			AddCadre($part_id, $user_id, $c_fname, $c_lname, $c_position, $c_contact, $c_phone, $c_m_phone, $c_sale, $c_limit);
		}else{
			SaveCadre($cadre_id, $user_id, $c_position, $c_contact, $c_phone, $c_m_phone, $c_sale, $c_limit);
		}
        break;
    case 'disable':
		$cadre_id 		= $_REQUEST['id'];
		DeleteCadre($cadre_id);			
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Partner Cadre Functions
 * ******************************
 */

function AddCadre($part_id, $user_id, $c_fname, $c_lname, $c_position, $c_contact, $c_phone, $c_m_phone, $c_sale, $c_limit)
{
	mysql_query("INSERT INTO `partner_contact_info`
					(`partner_id`, `user_id`, `f_name`, `l_name`, `position`, `phone`, `m_phone`, `chontacter`, `sale_rate`, `sale_limit`) 
				 VALUES
					($part_id, '$user_id', '$c_fname', '$c_lname', '$c_position', '$c_phone', '$c_m_phone', $c_contact, '$c_sale', '$c_limit')");
}

function SaveCadre($cadre_id, $user_id, $c_position, $c_contact, $c_phone, $c_m_phone, $c_sale, $c_limit)
{
	mysql_query("UPDATE
	    			`partner_contact_info`
				 SET
				    `user_id` = '$user_id',
				    `position` = '$c_position',
				    `chontacter` = $c_contact,
				    `phone` = '$c_phone',
				    `m_phone` = $c_m_phone,
				    `sale_rate` = $c_sale,
				    `sale_limit` = $c_limit
				 WHERE
					`id` = $cadre_id");
}

function DeleteCadre($cadre_id)
{
    mysql_query("DELETE 
				 FROM
	    			`partner_contact_info`
				 WHERE
					`id` = $cadre_id");
}	
		
function Contact($point)
{
	$data = '';
	
	switch ($point) {
	    case 0:
			$data = '<option value="1">კი</option>
					 <option value="0" selected="selected">არა</option>';
	        break;
	    case 1:
			$data = '<option value="1" selected="selected">კი</option>
					 <option value="0">არა</option>';
	        break;
	    default:
			$data = '<option value="1">კი</option>
					 <option value="0" selected="selected">არა</option>';
	}
	
	return $data;
}

function GetCadre($cadre_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`,
    											 `user_id`,
											     `f_name`,
											     `l_name`,
											     `position`,
											   	 `chontacter`,
											   	 `phone`,
											   	 `m_phone`,
											   	 `sale_rate`,
											   	 `sale_limit`
									      FROM `partner_contact_info`
									      WHERE `id` = $cadre_id"));
	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ახლის დამატება</legend>
	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="user_id">პირადი №</label></td>
					<td>
						<input type="text" id="user_id" class="idle user_id" onblur="this.className=\'idle user_id\'" onfocus="this.className=\'activeField user_id\'" value="' . $res['user_id'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="c_fname">სახელი</label></td>
					<td>
						<input type="text" id="c_fname" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['f_name'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="c_lname">გვარი</label></td>
					<td>
						<input type="text" id="c_lname" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['l_name'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="c_position">თანამდებობა</label></td>
					<td>
						<input type="text" id="c_position" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['position'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="c_contact">საკონტაქტო</label></td>
					<td>
						<select id="c_contact" class="idls small">' . Contact($res[chontacter]) . '</select>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="c_phone">ტელეფონი</label></td>
					<td>
						<input type="text" id="c_phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['phone'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="c_m_phone">მობილური</label></td>
					<td>
						<input type="text" id="c_m_phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['m_phone'] . '" />
					</td>
				</tr>
			</table>
	    </fieldset>
	    <fieldset>		    
	    	<legend>ფასდაკლება</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="c_sale">ფასდაკლების %</label></td>
					<td>
						<input type="text" id="c_sale" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['sale_rate'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="c_limit">ლიმიტი</label></td>
					<td>
						<input type="text" id="c_limit" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['sale_limit'] . '" />
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="cadre_id" value="' . $res['id'] . '" />
	    </fieldset>
    </div>
    ';
	return $data;
}

?>